//
//  CQPlaceHoldTextView.swift
//  swift-weibo
//
//  Created by 陈乾 on 15/11/4.
//  Copyright © 2015年 陈乾. All rights reserved.
//

import UIKit

class CQPlaceHoldTextView: UITextView {

    //给外界一个placeHold的字符串属性 让外界传值
    var placeHold: String? {
        didSet{
         placeHoldLabel.text = placeHold
        //外界给UITextView的font 赋值 我们就吧他也复制placeHoldLabel 这样字体就一样大了
         placeHoldLabel.font = font
        //设置大小
            placeHoldLabel.sizeToFit()
        }
    }
    
    
   //MARK: - 懒加载一个label当做placeHold
    lazy var placeHoldLabel: UILabel = {
        
        let label = UILabel()
        
        //设置颜色
        label.textColor = UIColor.lightGrayColor()
        
        return label
        }()
    //MARK: - 构造方法
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        
        super.init(frame: frame, textContainer: textContainer)
        //准备UI
        prepareUI()
        //创建通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", name: UITextViewTextDidChangeNotification, object: self)
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 通知回调
    /*
    {name = UITextViewTextDidChangeNotification; object = <swift_weibo.CQPlaceHoldTextView: 0x7fe26396c000; baseClass = UITextView; frame = (0 0; 375 623); text = 'Ff'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fe2650c7ac0>; layer = <CALayer: 0x7fe2650c9880>; contentOffset: {0, -64}; contentSize: {375, 38}>}

    */
    func textDidChange() {
       placeHoldLabel.hidden = hasText()
    
    }
    //MARK: - 析构
    deinit {
     NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    //MARK: - 准备UI
    func prepareUI() {
     //加载子控件
     addSubview(placeHoldLabel)
     //设置约束
     placeHoldLabel.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: nil, offset: CGPoint(x: 5, y: 8))
    }

}
